<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      visible: false,
      dataFormConfig: {
        deepOrderDetailId: {
          label: '订单',
          type: 'list',
          list: [],
          placeholder: '请输入订单',
          filterable: true,
          selectCallback: this.orderSelectCallback
        },
        deepProductId: {
          label: '产品',
          type: 'list',
          list: [],
          disabled: true,
          filterable: true,
          clearable: true,
          placeholder: '请输入产品'
        },
        putInTime: {
          label: '入库时间',
          type: 'date',
          showInMultiMode: true,
          placeholder: '请选择入库时间'
        },
        finishNumber: {
          label: '成品数',
          type: 'number',
          max: 100000000
        },
        lossNumber: {
          label: '损耗数',
          type: 'number',
          max: 100000000
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        }
      },
      editTableConfig: {
        orderDetailId: {
          title: '订单编号',
          type: 'select',
          list: this.orderDetailList,
          valueChangeEvent: this.changeOrderByOrderDetailId
        },
        productId: {
          title: '产品名称',
          type: 'select',
          optionsGet: () => {
            return this.productInfoList
          }
        },
        productNumber: {
          title: '成品数量',
          type: 'number',
          min: 0,
          default: 0
        },
        remark: {
          title: '备注'
        }
      }
    }
  },
  methods: {
    async init(id, multiAdd) {
      this.dataForm.id = id || 0
      this.initDeepProductInfoList()
      this.initDeepOrderDetailList('deepOrderDetailId')

      const props = {
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('deepProductId', 'finishNumber', 'putInTime'),
        initApi: 'deepApi/deepFinishAddInfo',
        saveApi: 'deepApi/saveDeepFinishAdd',
        updateApi: 'deepApi/updateDeepFinishAdd',
        objectName: 'deepFinishAdd'
      }

      if (multiAdd) {
        mRequest('orderApi/getDeepOrderDetailList', {}, res => {
          this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
        })
        mRequest('productApi/getProductInfoList', {}, res => {
          this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
        })

        props['multiSaveApi'] = 'deepApi/multipleDeepFinishAdd'
        props['editTableConfig'] = this.editTableConfig
      }

      this.$refs.dialog.initDialog(props)
    },
    changeOrderByOrderDetailId(row) {
      const {orderDetailId} = row

      if (this.R.isNil(orderDetailId) || this.R.equals('', orderDetailId)) {
        return
      }
      mRequest('orderApi/orderDetailInfo', {id: orderDetailId}, (data) => {
        const orderDetail = this.R.pathOr({}, ['data', 'orderDetail'], data)

        row.productId = orderDetail.productId
      })
    },
    orderSelectCallback() {
      const deepOrderDetailId = this.dataForm.deepOrderDetailId
      if (this.R.isNil(deepOrderDetailId) || this.R.isEmpty(deepOrderDetailId)) {
        return
      }
      mRequest('deepApi/deepOrderDetailInfo', {id: deepOrderDetailId}, ({data}) => {
        this.dataForm['deepProductId'] = this.R.pathOr(0, ['deepOrderDetail', 'deepProductId'], data)
        this.dataForm['clientId'] = this.R.pathOr(0, ['deepOrderDetail', 'clientId'], data)
      })
    }
  }
}
</script>
